package examples;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;

import debugs.UserDebug;

import search.UserSearchData;
import search.UserSearchEngine;

import models.UserModel;

import entities.User;

public class ExampleServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		
		
		String url = "http://www.statscrop.com/www/domainworlds.net";
		try {
			Connection conn = Jsoup.connect(url);
			Document doc = conn
					.header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					.header("accept-Language", "en-us,en;q=0.5")
					.header("referer", "http://www.webpagetest.org/")
					.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1")
					.followRedirects(true)
					.get();
			Response resp = conn.response();
			
			String html = resp.parse().html();
			System.out.println("a" + html + "b");
			if (html.isEmpty()) {
				response.getWriter().println("emtpy");
			} else {
				response.setContentType("text/html");
				response.setCharacterEncoding("UTF-8");
				response.getWriter().println(html);
			}
			
			
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
		/*UserModel userModel = UserModel.getInstance();
		User user = new User();
		
		user.setUsername("hoantocxoan@gmail.com");
		user.setName("Bui Ngoc Hoan");
		userModel.insert(user);
		UserSearchEngine.update(user);
		
		user = new User();
		user.setUsername("hoanbn1984@gmail.com");
		user.setName("Hoan Bui Ngoc");
		userModel.insert(user);
		UserSearchEngine.update(user);
		
		user = new User();
		user.setUsername("luckystart.2678@gmail.com");
		user.setName("Dao Thi Tham");
		userModel.insert(user);
		UserSearchEngine.update(user);*/
		
		/*String name = "Hoan";
		String username= "luckystart.2678@gmail.com";
		response.getWriter().println();
		response.getWriter().println("Search name " + name);
		response.getWriter().println();
		ArrayList<User> users = UserSearchData.searchName(name);
		for (int i = 0; i < users.size(); i++) {
			UserDebug.debugWeb(users.get(i), response);
		}
		
		response.getWriter().println();
		response.getWriter().println("Search username " + username);
		users = UserSearchData.searchUsername(username);
		for (int i = 0; i < users.size(); i++) {
			UserDebug.debugWeb(users.get(i), response);
		}*/
	}
}
